package com.shakespace.artofandroid.chapter07anim

import android.animation.*
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.shakespace.artofandroid.R
import com.shakespace.firstlinecode.global.TAG
import kotlinx.android.synthetic.main.activity_animator.*

class AnimatorActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animator)

        /**
         * 1. 作用的对象
         * 2. 对象的属性--- ObjectAnimator会寻找这个属性名对应的get和set方法
         * 3. 属性的变化值 如果不传值，报错，但是运行报错。
         * 4. 如果只有一个初始值， 会先调用setupSetterAndGetter(PropertyValuesHolder)，在该方法中通过反射调用get方法，获取当前属性值。
         */
//        val alphaAnimator = ObjectAnimator.ofFloat(iv_anim, "alpha", 1f, 0f, 1f)
        val alphaAnimator = ObjectAnimator.ofFloat(iv_anim, "alpha",0.5f)
        val rotationAnimator = ObjectAnimator.ofFloat(iv_anim, "rotation", 0f, 180f,270f,90f)
        val transAnimator = ObjectAnimator.ofFloat(iv_anim, "translationX", 500f, -200f, 100f, 0f)
        val scaleAnimator = ObjectAnimator.ofFloat(iv_anim, "scaleY", 0.2f, 2f)

        rotationAnimator.setEvaluator(MyFloatEvaluator())
//        rotationAnimator.interpolator = MyLinearInterpolator()
        rotationAnimator.interpolator = MyDecelerateInterpolator()

        tv_object_alpha.setOnClickListener {
            alphaAnimator.duration = 2000
            alphaAnimator.start()
        }
        tv_object_rotate.setOnClickListener {
            rotationAnimator.duration = 1600
            rotationAnimator.start()
        }

        tv_object_scale.setOnClickListener {
            scaleAnimator.duration = 2000
            scaleAnimator.start()
        }

        tv_object_trans.setOnClickListener {
            transAnimator.duration = 2000
            transAnimator.start()
        }

        tv_object_set.setOnClickListener {
            val animSet = AnimatorSet()
            animSet.play(alphaAnimator).with(transAnimator).with(scaleAnimator)
                .after(rotationAnimator)
            animSet.duration = 2000
            animSet.start()
        }

        // 监听
        alphaAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationRepeat(animation: Animator?) {
                Log.e(this.TAG, "onAnimationRepeat:  ")
            }

            override fun onAnimationEnd(animation: Animator?) {
                Log.e(this.TAG, "onAnimationEnd:  ")
            }

            override fun onAnimationCancel(animation: Animator?) {
                Log.e(this.TAG, "onAnimationCancel:  ")
            }

            override fun onAnimationStart(animation: Animator?) {
                Log.e(this.TAG, "onAnimationStart:  ")
            }
        })

        // AnimatorListenerAdapter 可以单独重写某一个方法
        transAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                Log.e(this.TAG, "onAnimationEnd:  ")
            }
        })

        rotationAnimator.addUpdateListener {
            // 屏幕每刷新一次，调用一次，通常是16ms 此时可以获得当前的属性值
            Log.e(this.TAG, "onUpdate: ${it.animatedValue} ")
        }

        tv_object_xml.setOnClickListener {
            val animator = AnimatorInflater.loadAnimator(this, R.animator.demo)
            animator.setTarget(iv_anim)
            animator.start()
        }

        iv_anim.setOnClickListener {
            Log.e(this.TAG, "onClick:  ")
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        overridePendingTransition(R.anim.act_back_enter, R.anim.act_back_exit)
    }
}
